<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <div>
    <el-row>
      <el-col :span="4" align="left" style="line-height: 36px;height: 36px">
        <span style="font-size: 14px;color: #747778">高管信息</span>
      </el-col>
    </el-row>
    <el-row style="justify-content: center">
      <el-button @click="addRow()"
                 style="margin: 8px 0px 8px 0px;padding: 6.2px 7px;font-size: 13px;line-height: 18px"
                 v-show="tableData.length === 0 && canEdit && editable" round>
        <i class="joys-icon-new" style="margin-right: 12px;margin-left: 4px"/>
        <span style="margin: 0px">添加高管信息</span>
      </el-button>
      <el-button type="primary"
                 @click="addRow()"
                 style="padding: 0px 18px 0px 8px;line-height: 36px;margin: 12px 0px"
                 v-show="editable && canEdit && !showForm && tableData.length > 0" round>
        <i class="joys-icon-addagain" style="margin-right: 18px;"/>
        <span style="margin: 0px;display: inline-block;font-size: 14px;">继续添加</span>
      </el-button>
      <span v-if="tableData.length === 0 && !editable">没有数据</span>

    </el-row>

    <transition name="el-zoom-in-top">
      <el-form :model="formData" ref="data"
               :class="{cardBackColor:!canEdit}"
               :rules="rules"
               style="text-align: left;padding-top: 28px"
               v-show="showForm" inline>
        <el-row style="padding:0px 12px 24px 12px">
          <el-col :md="8" :lg="6">
            <el-form-item label="姓名" prop="managerName">
              <ft-input :editable="!canEdit"
                        v-model="formData.managerName"
                        class="input"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="高管类型" prop="managerMemberType">
              <ft-data-enum-select v-model="formData.managerMemberType"
                                   :editing="!canEdit"
                                   type-name="managerType">
              </ft-data-enum-select>
            </el-form-item>
          </el-col>

          <el-col :md="8" :lg="6">
            <el-form-item label="证件类型" prop="managerIdentityType">
              <ft-enum-select v-model="formData.managerIdentityType"
                              :editing="!canEdit"
                              service-name="lss-customer"
                              type-name="personalType"
                              placeholder="请选择">
              </ft-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="证件号码" prop="managerIdentityNo">
              <ft-input :editable="!canEdit" v-model="formData.managerIdentityNo"
                        class="input"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="性别" prop="managerSex">
              <el-radio v-model="formData.managerSex" v-if="!canEdit" label="MALE" style="margin-left: 8px">男</el-radio>
              <el-radio v-model="formData.managerSex" v-if="!canEdit" label="FEMALE">女</el-radio>
              <span v-if="canEdit" style="padding: 8px 0px 8px 8px">{{convertSex(formData.managerSex)}}</span>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="最高学历" prop="managerEducationBackground">
              <ft-data-enum-select
                v-model="formData.managerEducationBackground"
                type-name="educationBackground"
                placeholder="请选最高学历"
                :editing="!canEdit">
              </ft-data-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="部门" prop="managerDept">
              <ft-input :editable="!canEdit" v-model="formData.managerDept"
                        class="input"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="职务" prop="managerJob">
              <ft-data-enum-select
                v-model="formData.managerJob"
                type-name="postLevel"
                placeholder="请选职务"
                :editing="!canEdit">
              </ft-data-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="联系电话" prop="managerTel">
              <ft-input :editable="!canEdit" v-model="formData.managerTel"
                        placeholder="请输入内容" type="phonenum"></ft-input>
            </el-form-item>
          </el-col>

          <el-col :md="24" :lg="24">
            <el-form-item label="工作简历" prop="managerResume">
              <ft-input class="input"
                        type="textarea"
                        :editable="!canEdit"
                        :autosize='{ minRows: 1, maxRows: 5}'
                        v-model="formData.managerResume">
              </ft-input>
            </el-form-item>
          </el-col>
        </el-row>

        <hr style="background-color: #EDEDED;border: none;height: 1px;margin: 0px"/>

        <el-row style="padding:16px 0px">
          <el-col align="right">
            <el-button size="mini"
                       @click="handleCancel()"
                       v-show="!canEdit">取消
            </el-button>
            <el-button size="mini"
                       type="primary"
                       @click="handleSave()"
                       style="margin-left: 24px;margin-right: 36px"
                       v-show="!canEdit">保存
            </el-button>
          </el-col>
          <el-col align="right" v-show="canEdit">
            <el-button size="mini"
                       style="margin-left: 24px;margin-right: 36px"
                       @click="handleCancel()">返回
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </transition>

    <transition name="el-zoom-in-top">
      <el-table :data="tableData"
                v-show="tableData.length > 0 && !showForm"
                tooltip-effect="light"
                class="tableClass">
        <el-table-column label="姓名" prop="managerName" min-width="120" align="left">
          <template slot-scope="scope">
            <ft-link-text @click="handleCard(scope.$index,scope.row)"
                          :text="scope.row.managerName">
            </ft-link-text>
          </template>
        </el-table-column>
        <el-table-column label="高管类型" prop="managerMemberType" min-width="120" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-data-enum-select v-model="scope.row.managerMemberType"
                                 type-name="managerType"
                                 placeholder="请选择"
                                 :editing="!canEdit">
            </ft-data-enum-select>
          </template>
        </el-table-column>
        <el-table-column label="证件类型" prop="managerIdentityType" min-width="120" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-enum-select v-model="scope.row.managerIdentityType"
                            :editing="!canEdit"
                            service-name="lss-customer"
                            type-name="personalType">
            </ft-enum-select>
          </template>
        </el-table-column>
        <el-table-column label="证件号码" prop="managerIdentityNo" min-width="120" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column label="联系电话" prop="managerTel" min-width="120" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column label="部门" prop="managerDept" min-width="120" align="left" show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="editable" label="操作" min-width="120" align="center">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-popover title="确认" placement="top" width="200" v-model="scope.row.delVisible">
              <p>数据删除后，将不能恢复，确认删除？</p>
              <div style="text-align:right; margin:0">
                <el-button size="small"
                           @click="scope.row.delVisible=false">取消
                </el-button>
                <el-button type="danger" size="small" @click="handleDelete(scope.row)">确定</el-button>
              </div>
              <span slot="reference" style="margin-left:.10rem">
                  <el-button type="text" size="small" style="color:#B94A48">删除
                  </el-button>
                </span>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </transition>
  </div>
</template>

<script>
  import {identityFormat} from "@/pages/pc/assets/js/validate/rule.js"

  export default {
    name: "CustManager",
    props: {
      customerId: {
        type : String,
        default : '',
      },
      editable: {
        type : Boolean,
        default : true,
      },
    },
    mounted() {
      this.load()
    },
    data() {
      //根据证件号码判断是否合法
      let validateCustNum = (rule, value, callback) => {
        if (this.formData.managerIdentityType == '') {
          callback(new Error('请选择证件类型'))
        } else {
          if (this.formData.managerIdentityNo && value && value != '') {
            if (!identityFormat(this.formData.managerIdentityType, value)) {
              callback(new Error('输入证件号码不合法'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        }
      };
      return {
        identityEnum: "personCertificateType",
        condition: [],
        tableEnumEdit: false,
        tableData: [],
        formData: {},
        formBack:{},
        showForm: {
          type: Boolean,
          default: false
        },
        canEdit: true,
        rules: {
          managerName: [
            {required: true,message: '姓名不能为空',trigger: 'blur'},
            {max:80,message:'不能超过80字符',trigger:'blur'}],
          managerType: [
            {required: true,message: '高管类型不能为空',trigger: 'change'},],
          managerIdentityType: [
            {required: true,message: '证件类型不能为空',trigger: 'change'},],
          managerIdentityNo: [
            {required: true,message: '证件号码不能为空',trigger: 'blur'},
            {validator: validateCustNum, trigger: 'blur'}],
          managerMemberType: [
            {required: true,message: '高管人员类别不能为空',trigger: 'change'},],
          managerTel:[
            {max:25,message:'不能超过25字符',trigger:'blur'}],
          managerDept:[
            {max:20,message:'不能超过20字符',trigger:'blur'}],
          managerResume:[
            {max:200,message:'不能超过200字符',trigger:'blur'}],
        },
      }
    },

    methods: {
      data() {
        return {}
      },
      load() {
        this.$http.get('/joys-rest/lss-customer/manager/' + this.customerId).then(
          resp => {
            this.tableData = resp.body;
            this.showForm = false
          },
          resp => {
            this.$message.error(resp.body)
          })
      },
      //列表编辑按钮
      handleEdit(row) {
        this.formData = JSON.parse(JSON.stringify(row));
        this.formBack = JSON.parse(JSON.stringify(row));
        this.canEdit = false;
        this.showForm = true
      },
      //列表卡片按钮
      handleCard(index, row) {
        this.formData = row;
        this.canEdit = true;
        this.showForm = true
      },
      //列表删除按钮
      handleDelete(val) {
        this.$http.delete('/joys-rest/lss-customer/manager/' + val.custManagerId).then(
          resp => {
            this.load();
            this.$message.success('删除成功!')
          },
          resp => {
            this.$message.error(resp.body)
          })
      },
      //列表取消
      handleCancel() {
        this.showForm = false;
        this.canEdit = true;
        this.$refs.data.clearValidate()
      },
      //列表增行
      addRow() {
        this.formData = {
          customerId: this.customerId,
          managerType: "PERSONAL",
          managerIdentityType: 'IDENTITY_CARD'
        };
        this.canEdit = false;
        this.showForm = true
      },
      //保存
      handleSave() {
        this.$refs.data.validate((valid) => {
          if (valid) {
            this.$http.put('/joys-rest/lss-customer/manager/', this.formData).then(
              resp => {
                this.canEdit = true;
                this.showForm = false;
                this.$message.success('保存成功!');
                this.load()
              }, resp => {
                this.$message.error(resp.body);
                this.canEdit = false
              })
          } else {
            return false
          }
        })
      },
      //table界面显示转换
      convertSex(val) {
        if ('MALE' == val) {
          return '男'
        }
        if ('FEMALE' == val) {
          return '女'
        }
        return val
      },
      //字数较多时，超出部分用...显示
      convertInputValue(val) {
        if (null != val && '' != val && val.length > 9) {
          return val.substr(0, 9) + '...'
        }
        return val
      }
    }
  }
</script>
<style>
  .cardBackColor {
    background-color: #F4F8F9;
    margin-bottom: 4px;
  }

  .tableClass .cell {
    word-break: keep-all;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
<style scoped>
  .input{
    overflow: auto;
    white-space: normal;
    word-wrap: break-word;
  }
</style>
